package org.jeecg.modules.rental.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.rental.entity.RentalPackage;
import org.jeecg.modules.rental.entity.RentalPackageLevel;
import org.jeecg.modules.rental.mapper.RentalPackageLevelMapper;
import org.jeecg.modules.rental.mapper.RentalPackageMapper;
import org.jeecg.modules.rental.service.IRentalPackageService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 新租赁套餐
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalPackageServiceImpl extends ServiceImpl<RentalPackageMapper, RentalPackage> implements IRentalPackageService {

    private final RentalPackageLevelMapper rentalPackageLevelMapper;

    @Override
    public IPage<RentalPackage> selectJoinPage(Page page, Wrapper wrapper) {
        IPage<RentalPackage> pageList = baseMapper.selectJoinPage(page, wrapper);
        if(ObjectUtil.isNotEmpty(pageList) && ObjectUtil.isNotEmpty(pageList.getRecords())){
            List<Long> levelIdlist = pageList.getRecords().stream().map(RentalPackage::getPackageLevel).collect(Collectors.toList());
            List<RentalPackageLevel> packageLevelList = rentalPackageLevelMapper.selectBatchIds(levelIdlist);
            pageList.getRecords().stream().forEach(item->{
                Optional<RentalPackageLevel>  packageLevelOptional = packageLevelList.stream()
                        .filter(a-> a.getId().equals(item.getPackageLevel())).findFirst();
                packageLevelOptional.ifPresent(a-> item.setPackageLevelName(a.getName()));
            });
        }
        return pageList;
    }
}
